package com.practicesuite.controllayer;

import java.awt.Dimension;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.DoctorOrder;
import com.practicesuite.systemlayer.Prescription;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.FlowLayout;

/**
 * Panel for filling out information to create a new doctor's order.
 * 
 * @author James
 *
 */
public class CreateDoctorsOrdersPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    private List<Doctor> doctors;
    private String[] doctorNames;
    private List<TreatmentRecord> treatments;
    private String[] visibleTreatments;
    
    private JTextField textFieldLabWork;
    private JTextField textFieldFollowUp;
    private JTextField textFieldOther;
    private JTextField textFieldPerscription;

    /**
     * Create the panel.
     * @throws Exception 
     */
    public CreateDoctorsOrdersPanel(MainWindow container, final User user) throws Exception {
        this.window = container;
        setPreferredSize(new Dimension(500, 400));
        
        doctors = SystemDriver.getDoctorList();
        doctorNames = new String[doctors.size() + 1];
        doctorNames[0] = "Select";
        for (int i = 1; i <= doctors.size(); i++) {
            doctorNames[i] = doctors.get(i - 1).getName();
        }
        
        treatments = SystemDriver.getAllTreatmentRecords();
        visibleTreatments = new String[treatments.size()];
        for (int i = 0; i < treatments.size(); i++) {
            TreatmentRecord curT = treatments.get(i);
            Doctor curD = SystemDriver.getDoctorById(curT.getTreatingDoctor());
            visibleTreatments[i] = curT.getDateTime().toString() + " (" + curD.getName() + ")" 
            + "(Tret. Record ID" + curT.getTreatRecId() + ")";
        }
        
        setLayout(new GridLayout(0, 1, 0, 0));
        
        JLabel lblRequestAppointment = new JLabel("Add Doctor's Orders");
        lblRequestAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblRequestAppointment.setFont(new Font("Tahoma", Font.PLAIN, 24));
        add(lblRequestAppointment);
        
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        add(panel);
        
        final JList treatmentList = new JList(visibleTreatments);
        treatmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        treatmentList.setFont(new Font("Tahoma", Font.PLAIN, 12));

        JScrollPane scrollPane_2 = new JScrollPane(treatmentList);
        scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane_2.setPreferredSize(new Dimension(415, 50));
        panel.add(scrollPane_2);
        
        /*JPanel panel = new JPanel();
        FlowLayout flowLayout_4 = (FlowLayout) panel.getLayout();
        flowLayout_4.setAlignment(FlowLayout.LEFT);
        add(panel);
        
        JLabel lblDate = new JLabel(" Date:");
        lblDate.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblDate);
        
        final JComboBox comboBoxMonth = new JComboBox();
        comboBoxMonth.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxMonth.setModel(new DefaultComboBoxModel(new String[] {"Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
        comboBoxMonth.setSelectedIndex(0);
        panel.add(comboBoxMonth);
        
        final JComboBox comboBoxDay = new JComboBox();
        comboBoxDay.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxDay.setModel(new DefaultComboBoxModel(new String[] {"Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
        panel.add(comboBoxDay);
        
        final JComboBox comboBoxYear = new JComboBox();
        comboBoxYear.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxYear.setModel(new DefaultComboBoxModel(years));
        panel.add(comboBoxYear);
        
        JLabel lblTime = new JLabel("     Time:");
        lblTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblTime);
        
        final JComboBox comboBoxTime = new JComboBox();
        comboBoxTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxTime.setModel(new DefaultComboBoxModel(new String[] {"Select", "8:00 am", "8:30 am", "9:00 am", "9:30 am", "10:00 am", "10:30 am", "11:00 am", "11:30 am", "12:00 pm", "12:30 pm", "1:00 pm", "1:30 pm", "2:00 pm", "2:30 pm", "3:00 pm", "3:30 pm", "4:00 pm", "4:30 pm", "5:00 pm", "5:30 pm", "6:00 pm", "6:30 pm"}));
        panel.add(comboBoxTime);*/
        
        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout_3 = (FlowLayout) panel_1.getLayout();
        flowLayout_3.setAlignment(FlowLayout.LEFT);
        add(panel_1);
        
        JLabel lblReasonForAppointment = new JLabel(" Treating Doctor:");
        lblReasonForAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblReasonForAppointment.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(lblReasonForAppointment);
        
        final JComboBox comboBoxDoctor = new JComboBox();
        comboBoxDoctor.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxDoctor.setModel(new DefaultComboBoxModel(doctorNames));
        panel_1.add(comboBoxDoctor);
        
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
        flowLayout_2.setAlignment(FlowLayout.LEFT);
        add(panel_2);
        
        JLabel lblAdditionalNotesoptional = new JLabel(" Perscription:");
        lblAdditionalNotesoptional.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_2.add(lblAdditionalNotesoptional);
        
        textFieldPerscription = new JTextField();
        panel_2.add(textFieldPerscription);
        textFieldPerscription.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldPerscription.setColumns(15);
        
        JPanel panel_3 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        add(panel_3);
        
        JLabel lblDesiredDoctor = new JLabel(" Lab Work:");
        lblDesiredDoctor.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(lblDesiredDoctor);
        
        textFieldLabWork = new JTextField();
        textFieldLabWork.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(textFieldLabWork);
        textFieldLabWork.setColumns(15);
        
        JPanel panel_5 = new JPanel();
        add(panel_5);
        panel_5.setPreferredSize(new Dimension(500, 25));
        panel_5.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        
        JLabel lblBloodPressure = new JLabel(" Follow-up:");
        panel_5.add(lblBloodPressure);
        lblBloodPressure.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldFollowUp = new JTextField();
        panel_5.add(textFieldFollowUp);
        textFieldFollowUp.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldFollowUp.setColumns(15);
        
        JPanel panel_6 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_6.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        add(panel_6);
        panel_6.setPreferredSize(new Dimension(500, 75));
        
        JLabel lblTempurature = new JLabel(" Other Instructions:");
        panel_6.add(lblTempurature);
        lblTempurature.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldOther = new JTextField();
        panel_6.add(textFieldOther);
        textFieldOther.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldOther.setColumns(15);
        
        JPanel panel_4 = new JPanel();
        add(panel_4);
        
        JButton btnAddOrders = new JButton("Add Orders");
        btnAddOrders.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                TreatmentRecord treatment;
                int treatmentIndex = treatmentList.getSelectedIndex();
                try {
                    treatment = treatments.get(treatmentIndex);
                    
                    String doctorName = (String)comboBoxDoctor.getSelectedItem();
                    
                    Prescription prescription = new Prescription(null, textFieldPerscription.getText());
                    ArrayList<Prescription> a = new ArrayList<Prescription>();
                    a.add(prescription);
                    String labWork = textFieldLabWork.getText();
                    ArrayList<String> b = new ArrayList<String>();
                    b.add(labWork);
                    String followUpInstructions = textFieldFollowUp.getText();
                    String otherInstructions = textFieldOther.getText();
                    
                    if (doctorName.equals("Select")) {
                        JOptionPane.showMessageDialog(null, "Please select a treating doctor.");
                    }
                    else {
                        Doctor treatingDoctor = doctors.get(comboBoxDoctor.getSelectedIndex() - 1);
                        DoctorOrder doctorOrder = new DoctorOrder(a, b, followUpInstructions, 
                                otherInstructions, treatingDoctor.getUserID(), 0);
                        SystemDriver.createDoctorsOrders(doctorOrder, treatment.getTreatRecId());
                        window.gotoPanel("doctorHome", user);
                    }
                } catch (IndexOutOfBoundsException e) {
                    JOptionPane.showMessageDialog(null, "Please select a treatment record.");
                }
                
            }
        });
        panel_4.add(btnAddOrders);
        btnAddOrders.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("doctorHome", user);
            }
        });
        btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_4.add(btnCancel);

    }

}
